﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InstantSpine.Core.MaterialModels;

namespace InstantSpine.Core.ParaSpine
{
    static class APDLScriptEx
    {
        /// <summary>
        /// Write material test data for marlow uniaxial material
        /// to a 2-column (stress, strain) APDL array
        /// </summary>
        /// <param name="script">APDL script to be modified</param>
        /// <param name="materialTestdata">Material test data: (stress, strain) pairs</param>
        /// <param name="arrayParameterName">Name of the resulting array parameter</param>
        public static void MaterialTestdataToArray(this APDLScript script, MarlowUniaxialMaterial materialTestdata, string arrayParameterName) {
            if (materialTestdata.UniaxialTestData.Count > 0) {
                script.DefineArray2D(arrayParameterName, materialTestdata.UniaxialTestData.Count, 2);

                script.Comment(" Stresses");
                int testdataNo = 0; // row index in pname array
                foreach (var testdata in materialTestdata.UniaxialTestData) {
                    ++testdataNo;
                    script.Assign(arrayParameterName + "(" + testdataNo + ", 1)", testdata.Stress);
                }

                script.Comment(" Strains");
                testdataNo = 0; // row index in pname array
                foreach (var testdata in materialTestdata.UniaxialTestData) {
                    ++testdataNo;
                    script.Assign(arrayParameterName + "(" + testdataNo + ", 2)", testdata.Strain);
                }
            }
        }

        /// <summary>
        /// Write material test data for nonlinear springs
        /// to a 2-column (stress, strain) APDL array
        /// </summary>
        /// <param name="script">APDL script to be modified</param>
        /// <param name="materialTestdata">Material test data: (force, deflection) pairs</param>
        /// <param name="arrayParameterName">Name of the resulting array parameter</param>
        public static void MaterialTestdataToArray(this APDLScript script, NonlinearSpringBehavior materialTestdata, string arrayParameterName) {
            if (materialTestdata.ForceDeflectionCurve.Count > 0) {
                script.DefineArray2D(arrayParameterName, materialTestdata.ForceDeflectionCurve.Count, 2);

                script.Comment(" Force");
                int testdataNo = 0; // row index in pname array
                foreach (var testdata in materialTestdata.ForceDeflectionCurve) {
                    ++testdataNo;
                    script.Assign(arrayParameterName + "(" + testdataNo + ", 1)", testdata.Force);
                }

                script.Comment(" Deflection");
                testdataNo = 0; // row index in pname array
                foreach (var testdata in materialTestdata.ForceDeflectionCurve) {
                    ++testdataNo;
                    script.Assign(arrayParameterName + "(" + testdataNo + ", 2)", testdata.Deflection);
                }
            }
        }
    }
}
